var express = require('express');
var router = express.Router();
var Classify = require('../model/article_classify');
// 增加分类
require('./../utils/dateFormat');
router.get('/add', async (req, res) => {
  var {
    userId,
    classifyName
  } = req.query;
  try {
    if (!userId || !classifyName) {
      res.json({
        resultCode: '0002',
        resultMessage: '参数错误',
        result: ''
      })
    } else {
      const exist = await Classify.findOne({
        userId,
        classifyName
      })
      if (exist) {
        res.json({
          resultCode: '0001',
          resultMessage: '分类已存在',
          result: ''
        })
      } else {
        let createTime = new Date().Format('yyyy-MM-dd hh:mm:ss');
        let s1 = Math.floor(Math.random() * 10);
        let s2 = Math.floor(Math.random() * 10);
        var classifyId = `${s1}${(Date.parse(new Date())) / 1000}${s2}`;
        Classify.insertMany({
          classifyName: classifyName,
          userId: userId,
          classifyId: classifyId,
          createTime: createTime
        })
        res.json({
          resultCode: '0000',
          resultMessage: '添加成功',
          result: ''
        })
      }
    }
  } catch (err) {
    res.json({
      resultCode: '9999',
      resultMessage: err.message,
      result: ''
    })
  }
})

// 查询分类
router.get('/search', async (req, res) => {
  var {
    userId
  } = req.query
  try {
    if (!userId) {
      res.json({
        resultCode: '0002',
        resultMessage: '参数错误',
        result: ''
      })
    } else {
      const exist = await Classify.find({
        userId
      })
      if (exist) {
        res.json({
          resultCode: '0000',
          resultMessage: '查询成功',
          result: exist
        })
      } else {
        res.json({
          resultCode: '0000',
          resultMessage: '查询成功',
          result: []
        })
      }
    }
  } catch (err) {
    res.json({
      resultCode: '9999',
      resultMessage: err.message,
      result: ''
    })
  }
})

// 修改分类名
router.get('/edit', async (req, res) => {
  var {
    newClassifyName,
    oldClassifyName,
    userId
  } = req.query
  try {
    if (!newClassifyName || !oldClassifyName || !userId) {
      res.json({
        resultCode: '0001',
        resultMessage: '参数错误',
        result: ''
      })
    } else {
      if (newClassifyName === oldClassifyName) {
        res.json({
          resultCode: '0000',
          resultMessage: '修改成功',
          result: ''
        })
      } else {
        const repeat = await Classify.findOne({
          classifyName: newClassifyName,
          userId: userId
        });
        if (repeat) {
          res.json({
            resultCode: '0003',
            resultMessage: '名称重复',
            result: ''
          })
        } else {
          await Classify.updateOne({
            classifyName: oldClassifyName,
            userId: userId
          }, {
            classifyName: newClassifyName
          })
          res.json({
            resultCode: '0000',
            resultMessage: '修改成功',
            result: ''
          })
        }
      }
    }
  } catch (err) {
    res.json({
      resultCode: '9999',
      resultMessage: err.message,
      result: ''
    })
  }
})

// 删除分类
router.get('/delete', async (req, res) => {
  const { classifyId, userId } = req.query;
  try {
    if (!classifyId || !userId) {
      res.json({
        resultCode: '0001',
        resultMessage: '参数错误',
        result: ''
      })
    } else {
      await Classify.deleteOne({
        userId,
        classifyId
      })
      res.json({
        resultCode: '0000',
        resultMessage: '删除成功',
        result: ''
      })
    }
  } catch(err) {

  }
  
})
module.exports = router